package com.yzy.fly.service.util;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yzy.fly.annotation.OrderSeq;
import com.yzy.fly.annotation.ParentId;
import com.yzy.fly.kernel.model.MetadataModel;
import com.yzy.fly.kernel.model.ParentIdType;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Deprecated
public class MetadataModelUtil {

    public static String getTableName(Object object){
        return getTableName(object.getClass());
    }

    public static String getTableName(Class<?> cls){
        String tableName = "";
        if (cls.isAnnotationPresent(TableName.class)) {
            TableName annotation = cls.getAnnotation(TableName.class);
            tableName = annotation.value();
        }
        return tableName;
    }

    private static Object getSeqValue(Object object){
        Class cls = object.getClass();
        Object seq = null;
        //找seq method比field优先
        List<Method> methods = MethodInvokeUtil.findMethodAnnotationPresent(cls, OrderSeq.class);
        if (CollectionUtils.isNotEmpty(methods)) {
            Method method = methods.get(0);
            seq = MethodInvokeUtil.invoke(object, method);
        } else {
            List<Field> orderSeqFields = FieldInvokeUtil.findFieldsByAnnotationPresent(cls, OrderSeq.class);
            if (CollectionUtils.isNotEmpty(orderSeqFields)) {
                Field orderSeqField = orderSeqFields.get(0);
                Method getterSeqMethod = MethodInvokeUtil.findGetterMethodByField(cls, orderSeqField);
                seq = MethodInvokeUtil.invoke(object, getterSeqMethod);
            }
        }
        return seq;
    }

    public static String getSeqColumnName(Object object){
        return getSeqColumnName(object.getClass());
    }
    public static String getSeqColumnName(Class<?> cls){
        String columnName = "";
        List<Field> orderSeqFields = FieldInvokeUtil.findFieldsByAnnotationPresent(cls, OrderSeq.class);
        if (CollectionUtils.isNotEmpty(orderSeqFields)) {
            Field field = orderSeqFields.get(0);
            boolean annotationTableFieldPresent = field.isAnnotationPresent(TableField.class);
            if (annotationTableFieldPresent) {
                TableField tableFieldAnn = field.getAnnotation(TableField.class);
                if (tableFieldAnn.exist()) {
                    columnName = tableFieldAnn.value();
                }
            }
            if (StringUtils.isBlank(columnName)) {
                columnName = field.getName();
            }
        }
        return columnName;
    }

    public static String getIdColumName(Object object){
        return getIdColumName(object.getClass());
    }
    public static String getIdColumName(Class<?> cls){
        String columName = "";
        List<Field> fields = FieldInvokeUtil.findFieldsByAnnotationPresent(cls, TableId.class);
        if (CollectionUtils.isNotEmpty(fields)) {
            Field field = fields.get(0);
            boolean annotationTableFieldPresent = field.isAnnotationPresent(TableField.class);
            if (annotationTableFieldPresent) {
                TableField tableFieldAnn = field.getAnnotation(TableField.class);
                if (tableFieldAnn.exist()) {
                    columName = tableFieldAnn.value();
                }
            }
            if (StringUtils.isBlank(columName)) {
                columName = field.getName();
            }
        }
        return columName;
    }

    public static String getParentIdColumName(Object object){
        return getParentIdColumName(object.getClass());
    }

    public static String getParentIdColumName(Class<?> cls){
        String columnName = "";
        List<Field> fields = FieldInvokeUtil.findFieldsByAnnotationPresent(cls, ParentId.class);
        if (CollectionUtils.isNotEmpty(fields)) {
            Field field = fields.get(0);
            boolean annotationTableFieldPresent = field.isAnnotationPresent(TableField.class);
            if (annotationTableFieldPresent) {
                TableField tableFieldAnn = field.getAnnotation(TableField.class);
                if (tableFieldAnn.exist()) {
                    columnName = tableFieldAnn.value();
                }
            }
            if (StringUtils.isBlank(columnName)) {
                columnName = field.getName();
            }
        }
        return columnName;
    }

    public static void getParentIdColumNames(Class<?> cls,MetadataModel metadataModel){
        List<Field> fields = FieldInvokeUtil.findFieldsByAnnotationPresent(cls, ParentId.class);
        if (CollectionUtils.isNotEmpty(fields)) {
//            List<String> inColumnNames = Lists.newArrayListWithCapacity(0);
//            List<String> outColumnNames = Lists.newArrayListWithCapacity(0);
            List<String> inColumnNames = metadataModel.getInsideParentIdColumnNames();
            List<String> outColumnNames = metadataModel.getOutsideParentIdColumnNames();

            Map<String,String> inColumnNamesMap = metadataModel.getInsideParentIdColumnNamesMap();
            Map<String,String> outColumnNamesMap = metadataModel.getOutsideParentIdColumnNamesMap();

            for (int i = 0; i < fields.size(); i++) {
                String columnName = "";
                Field field =  fields.get(i);
                ParentId annotationParentId = field.getAnnotation(ParentId.class);
                boolean annotationTableFieldPresent = field.isAnnotationPresent(TableField.class);
                if (annotationTableFieldPresent) {
                    TableField tableFieldAnn = field.getAnnotation(TableField.class);
                    if (tableFieldAnn.exist()) {
                        columnName = tableFieldAnn.value();
                    }
                }
                if (StringUtils.isBlank(columnName)) {
                    columnName = annotationParentId.value();
                }

                String fieldName = field.getName();
                if (StringUtils.isBlank(columnName)) {
                    columnName = fieldName;
                }

                Map<String,String> columnNameMap = new HashMap<>(2);

                ParentIdType parentIdType = annotationParentId.type();
                if(ParentIdType.INSIDE == parentIdType){
                    inColumnNames.add(columnName);
                    inColumnNamesMap.put(columnName,fieldName);
                } else if(ParentIdType.OUTSIDE == parentIdType){
                    outColumnNames.add(columnName);
                    outColumnNamesMap.put(columnName,fieldName);
                }
            }
        }
    }
}
